package ObjGitModel

import (
	"arabApi/app/api/models"
	"arabApi/app/api/requests/ObjGitRequest"
	"arabApi/pkg/database"
	"arabApi/pkg/helpers"
)

// TpObjJobPath  项目地址
type ObjJobPath struct {
	ID    int64  `gorm:"column:id" db:"id" json:"id"`
	JobId int64  `gorm:"column:job_id" db:"job_id" json:"job_id"` //  项目id
	Step  int64  `gorm:"column:step" db:"step" json:"step"`       //   2 测试环境 3 开发环境 4 正式环境
	Ip    string `gorm:"column:ip" db:"ip" json:"ip"`             //  发布ip
	Path  string `gorm:"column:path" db:"path" json:"path"`       //  项目存放地址
	Desc  string `gorm:"column:desc" db:"desc" json:"desc"`       //  描述
	Port  string `gorm:"column:port" db:"desc" json:"port"`       //  端口
	models.CommonTimestampsField
}

func (ObjJobPath) TableName() string {
	return "tp_obj_job_path"
}

func PathList(job_id int64) []ObjJobPath {
	var list []ObjJobPath
	database.DB.Model(ObjJobPath{}).Where("job_id = ?", job_id).Order("step").Find(&list)
	return list
}

func PathMsgByid(r int64) ObjJobPath {
	var msg ObjJobPath
	database.DB.Where("id = ?", r).Find(&msg)
	return msg
}

func PathAdd(r ObjGitRequest.ObjGitPushPath) (int64, error) {
	s := &ObjJobPath{}

	if err := helpers.Copy(r, s); err != nil {
		return 0, err
	}
	res := database.DB.Model(s).Create(s)
	return s.ID, res.Error
}

func PathEdit(r ObjGitRequest.ObjGitPushPath) error {
	s := &ObjJobPath{}

	if err := helpers.Copy(r, s); err != nil {
		return err
	}
	res := database.DB.Model(s).Where("id = ?", r.ID).Updates(s)
	return res.Error
}

func Group(job_id int64, step int64) []ObjJobPath {
	var list []ObjJobPath
	database.DB.Model(ObjJobPath{}).Where("job_id = ?", job_id).Where("step = ?", step).Order("step").Find(&list)
	return list
}
